

var QImageGallery = QWebControl.extend('QImageGallery', {
	
	_slideSwitch : null,

	load : function()
	{
		this.jQuery().css("overflow", "hidden");
		
		var ref_this = this;
		this._slideSwitch = function() {ref_this.slideSwitch()};
		setTimeout(this._slideSwitch, 4000);
	},
	
	slideSwitch : function ()
	{
		var $active = this.jQuery(".___QImageGallery .___QImageGallery_div.active");
		
		if ( $active.length == 0 ) $active = this.jQuery(".___QImageGallery .___QImageGallery_div:last");
		
		// use this to pull the images in the order they appear in the markup
		var $next =  $active.next().length ? $active.next() : this.jQuery(".___QImageGallery .___QImageGallery_div:first");
		
		// uncomment the 3 lines below to pull the images in random order
		
		// var $sibs  = $active.siblings();
		// var rndNum = Math.floor(Math.random() * $sibs.length );
		// var $next  = $( $sibs[ rndNum ] );
		
		$active.addClass('last-active');
		
		var ref_this = this;
		this._slideSwitch = function() {ref_this.slideSwitch()};
		
		$next.css({opacity: 0.0})
			.addClass('active')
			.animate({opacity: 1.0}, 1000, function() {
			    $active.removeClass('active last-active');
			    
			    setTimeout(ref_this._slideSwitch, 4000);
			});
	},
	
	destroyControl : function()
	{
		// alert("clearTimeout");
		if (this._slideSwitch)
			clearTimeout(this._slideSwitch);
	}

});

